<template>
    <div class="container-box">
        <div style="height: 98px;"></div>
        <!-- 查看照片 -->
        <morePicture :url="currentImage" :showimage="showimage" :deg="deg" :num="num" :isfirst="isfirst"
            :islast="islast" :imglist="imgList" @closeimage="closeimage">
        </morePicture>
        <!-- serch -->
        <div class="bgfffs filter_box" :style="contentStyleObj">
            <page-filter :className="className" lableWidth="70px" ref="from" :query.sync="filterInfo.query"
                :filter-list="filterInfo.list" :list-type-info="listTypeInfo" @handleClick="handleClick"
                @handleEvent="handleEvent" @upenter="upenter">
            </page-filter>
        </div>

        <div class="bgfff">
            <div class="tableHeight" ref="tableWrapper" style="padding: 10px 10px">
                <page-table :checkBox="false" ref="table" :tabIndex="true" :dataText="dataText" v-loading="loading"
                    :init-curpage="tableInfo.initCurpage" :data.sync="tableInfo.data" :pageParm="pageParm"
                    :query="filterInfo.query" :field-list="tableInfo.fieldList" :list-type-info="listTypeInfo"
                    :handle="tableInfo.handle" @handleClick="handleClick" @handleEvent="handleEvent">

                    <template v-slot:col-prove_img="scope">
                        <i v-if="scope.row.prove_img.length > 0" class="el-icon-picture addicon"
                            @click.stop="openFile(scope.row.prove_img)"></i>
                        <p v-else> - </p>
                    </template>
                </page-table>
            </div>
        </div>

        <page-dialog :title="dialogInfo.title[dialogInfo.type]" :visible.sync="dialogInfo.visible"
            :width="dialogInfo.width" :bt-loading="dialogInfo.btLoading"
            :bt-list="is_look ? dialogInfo.btList2 : dialogInfo.btList" shutEvent="close" @handleClick="handleClick"
            @handleEvent="handleEvent">
            <template v-slot>
                <!-- 添加 修改-->
                <el-form v-if="dialogInfo.type == 'edit' || dialogInfo.type == 'look'" label-position="left"
                    :inline="false" :model="form" ref="form" :rules="rules" class="user_form addFrom"
                    label-width="110px">
                    <el-form-item label="帮扶状态：" prop="status" autocomplete="off">
                        <el-select popper-class="select-css" class="selectcss" :disabled="dialogInfo.type == 'look'"
                            filterable v-model="form.status" clearable placeholder="请选择帮扶状态" @change="changeRole">
                            <el-option v-for="item in listTypeInfo.status_list" :key="item.key" :label="item.value"
                                :value="item.key">
                                <!-- :value="{ id: item.key, value: item.value }" -->
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item v-if="form.status == '20'" label="帮扶金额：" prop="amount">
                        <el-input v-model="form.amount" :disabled="dialogInfo.type == 'look'" @input="handleInput"
                            clearable style="width: 200px" placeholder="请输入帮扶金额" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="备注：" style="height: 120px !important;">
                        <el-input v-model="form.remark" :disabled="dialogInfo.type == 'look'" class="remark"
                            style="width: 350px; height: 70px !important;" placeholder="请输入备注" type="textarea"
                            :autosize="{ minRows: 2, maxRows: 8 }" auto-complete="off" clearable
                            autocomplete="off"></el-input>
                    </el-form-item>
                </el-form>
            </template>
        </page-dialog>
    </div>
</template>
<script>
import PageDialog from "@/components/PageDialog";
import PageFilter from "@/components/PageFilter/index";
import PageTable from "@/components/PageTable/index";
import morePicture from "@/components/morePicture";
import { formatter } from "@/utils/index";
import {
    getFundAssist,
    searchFundData,
    setFundStatus,
} from '@/api/api'
export default {
    name: "paymentPage",
    components: {
        PageFilter,
        PageDialog,
        PageTable,
        morePicture
    },
    data () {
        return {
            is_look: false,
            // 图片预览
            dialogVisible: false,
            currentImage: '',
            url: "",
            deg: 0,
            num: 0,
            isfirst: false,
            islast: false,
            imgList: [],
            showimage: false,
            loading: false,
            dataText: "",
            contentStyleObj: {
                height: "100%"
            },
            namebool: true,
            refresh: false,
            showPassword: false,
            className: "forms",
            is_super_admin: 0,
            msg: "",
            form: {
                id: '',
                status: '',
                remark: '',
                amount: '',
            },
            // 权限列表
            permissionLists: [],
            // 选中权限
            checkList: [1],
            role_id_list: [
                {
                    key: 1,
                    value: "管理员"
                },
                {
                    key: 2,
                    value: "操作员"
                },
            ],
            rules: {
                amount: [
                    {
                        required: true,
                        trigger: "blur",
                        message: "请输入帮扶金额"
                    }
                ],
                status: [
                    {
                        required: true,
                        trigger: "change",
                        message: "请选择帮扶状态"
                    }
                ],
            },
            // 相关列表
            listTypeInfo: {
                user_list: [], //申请人列表
                status_list: [], //状态
            },

            // 弹窗相关
            dialogInfo: {
                width: "550px",
                title: {
                    edit: "设置",
                    look: "查看"
                },
                visible: false,
                type: "",
                btLoading: false,
                btList: [
                    {
                        label: "取消",
                        event: "close",
                        show: true
                    },
                    {
                        label: "确定",
                        type: "primary",
                        event: "save",
                        show: true
                    }
                ],
                btList2: [
                    {
                        label: "关闭",
                        type: "primary",
                        event: "close",
                        show: true
                    }
                ]
            },
            // 过滤相关配置
            filterInfo: {
                query: {
                    page: 1,
                    limit: 20,
                    name: "",
                    status: "",
                    phone: ""
                },
                list: [
                    {
                        type: "select",
                        label: "申请人",
                        value: "name",
                        list: "user_list",
                        key: "申请人"
                    },
                    {
                        type: "input",
                        label: "手机号",
                        value: "phone",
                        key: "手机号"
                    },
                    {
                        type: "select",
                        label: "状态",
                        value: "status",
                        list: "status_list",
                        key: "状态"
                    },
                    {
                        type: "button",
                        label: "搜索",
                        btType: "primary",
                        event: "search",
                        show: true
                    },
                    {
                        type: "button",
                        label: "重置",
                        event: "reset",
                        show: true
                    }
                ]
            },
            // 分页参数
            pageParm: {
                tableHeight: "52vh", // 表格最大高度
                total: 0, // 总条数
                loading: true, // 加载动画
                pagesizes: [20, 50, 100], // 分页数量列表
                query: {
                    // 查询条件
                    page: 1, // 当前页
                    limit: 20 // 每页条数
                }
            },
            // 表格相关
            tableInfo: {
                refresh: 1,
                initCurpage: 1,
                data: [],
                fieldList: [
                    { label: "申请人", value: "name", width: "100px" },
                    { label: "联系方式", value: "phone", width: "100px" },
                    { label: "申请原因", value: "reason", type: "popover" },
                    { label: "图片", value: "prove_img", type: 'slot', width: "100px" },
                    { label: "申请时间", value: "add_time", width: "130px" },
                    { label: "帮扶状态", value: "status_text", width: "100px" },
                    { label: "操作人", value: "admin_name", width: "100px" },
                    { label: "操作时间", value: "update_time", width: "130px" },
                ],
                handle: {
                    label: "操作",
                    width: "200px",
                    btList: [
                        {
                            label: "设置",
                            type: "text",
                            icon: "el-icon-browse",
                            event: "modify",
                            show: true,
                            ifRender: row => {
                                if (row.status == 10 || row.status == 15) {
                                    return true;
                                } else {
                                    return false;
                                }
                            }
                        },
                        {
                            label: "查看",
                            type: "text",
                            icon: "el-icon-browse",
                            event: "look",
                            show: true,
                            ifRender: row => {
                                if (row.status == 5 || row.status == 20) {
                                    return true;
                                } else {
                                    return false;
                                }
                            }
                        },

                    ]
                },
            },
        };
    },
    created () {
        this.init({ ...this.pageParm.query, ...this.filterInfo.query });
        this.getParamList();
        // this.getHeight();
        this.fetTableHeight()
        this.upenter();
        //window.addEventListener("resize", this.getHeight);
    },
    //enter事件 与点击查询按钮功能一致
    updated () {
        var that = this;
    },
    activated () {
        this.init(this.filterInfo.query);
        this.getParamList();
        this.upenter();
        this.refresh = false;
    },
    deactivated () {
        this.refresh = true;
    },
    filters: {
        formatter (time) {
            let date = new Date(time),
                text = "";
            if (time !== "") return (text = formatter(date, "yyyy-MM"));
            return text;
        }
    },
    computed: {
    },

    methods: {
        addUser () {
            this.checkList = [1]
            this.is_super_admin = 0
            this.form.id = 0,
                this.form.real_name = "",
                this.form.phone = "",
                this.form.pwd = "",
                this.form.role_id = "",
                this.dialogInfo.type = "add";
            this.dialogInfo.visible = true;
            this.$nextTick(() => {
                this.$refs.form.clearValidate();
            });
        },

        openFile (img) {
            console.log("--->", img);
            // // window.open(img, "_blank");
            // this.currentImage = img; // 设置当前图片
            // // this.dialogVisible = true; // 显示模态框
            // this.dialogInfo.type == 'lookImage'
            // this.dialogInfo.visible = true;
            if (img) {
                this.currentImage = img[0];
                this.imgList = img;
                this.showimage = true;
                this.deg = 0;
                this.num = 0;
                this.isfirst = false;
                this.islast = false;
            }
        },
        closeimage () {
            this.showimage = false;
        },
        handleInput (value) {
            let filtered = value
                .replace(/[^0-9.]/g, '')       // 只保留数字和小数点
                .replace(/^\./g, '')           // 不能以小数点开头
                .replace(/\.{2,}/g, '.')       // 不能连续输入多个小数点
                .replace(/(\..*)\./g, '$1');   // 只能有一个小数点

            // 特殊处理纯0或0开头的情况
            if (filtered === '0' || /^0+$/.test(filtered)) {
                filtered = '';  // 直接清空纯0输入
            } else if (/^0[^.]+/.test(filtered)) {
                filtered = filtered.replace(/^0+/, ''); // 去除开头的0（非小数情况）
            }

            this.form.amount = filtered.substring(0, 9);  // 限制总长度
        },
        // 更换角色
        changeRole (data) {
            console.log('角色---', data)
            if (data) {
                this.form.role_id = data.id;
                this.form.role_name = data.value;
            } else {
                this.form.role_id = "";
                this.form.role_name = "";
            }
        },
        unique (arr) {
            const res = new Map();
            return arr.filter(a => !res.has(a) && res.set(a, 1));
        },
        arrayAminusB (arrA, arrB) {
            return arrA.filter(v => !arrB.includes(v));
        },
        //enter 事件防止全局触发
        upenter () {
            var that = this;
            document.onkeydown = e => {
                //13表示回车键，baseURI是当前页面的地址，为了更严谨，也可以加别的，可以打印e看一下
                if (e.keyCode === 13 && e.target.baseURI.match("receivableVerification") && !that.showInvoice && !that.showReconciliation && !that.showDestroy && !that.dialogInfo.visible) {
                    console.log("回车事件", that.dialogInfo.visible);
                    //回车后执行搜索方法
                    that.filterInfo.query.page = 1;
                    that.pageParm.query.page = 1;
                    that.init(that.filterInfo.query);
                }
            };
        },

        getHeight () {
            if (this.contentStyleObj.height == "100%") {
                var height =
                    window.innerHeight - parseInt(this.contentStyleObj.height) - 160;
            } else {
                var height =
                    window.innerHeight - parseInt(this.contentStyleObj.height) - 146;
            }
            this.pageParm.tableHeight = height + "px";
        },
        // 重置table高度
        resetHeight () {
            return new Promise((resolve, reject) => {
                this.tableH = 0
                resolve()
            })
        },
        // 设置table高度
        fetTableHeight () {
            this.resetHeight().then(res => {
                this.pageParm.tableHeight = this.$refs.tableWrapper.getBoundingClientRect().height - 64
                // 设置最小高度，确保表格可以正常显示
                // this.tableH = Math.max(containerHeight - 10, 200); // 最小300px
            })
        },

        init (data) {
            this.loading = true;
            let formData = new FormData();
            formData.append('status', data.status);
            formData.append('name', data.name);
            formData.append('phone', data.phone);
            formData.append('page', data.page);
            formData.append('limit', data.limit);
            getFundAssist(formData).then(res => {
                this.tableInfo.data = [];
                this.tableInfo.data = res.data.data;
                if (this.tableInfo.data.length === 0) {
                    this.dataText = "暂无数据";

                }

                this.pageParm.total = res.data.meta.total;
                this.loading = false;
            });

        },

        getParamList () {
            searchFundData().then(res => {
                let {
                    name,
                    status,
                } = res.data;

                name = name.map(v => {
                    return {
                        key: v.value,
                        value: v.name
                    };
                });
                status = status.map(v => {
                    return {
                        key: v.value,
                        value: v.name
                    };
                });

                this.listTypeInfo.user_list = name;
                this.listTypeInfo.status_list = status;
            });

        },

        handleClick (event, data) {
            // console.log(event, data);
            // console.log('当前事件---', data)
            switch (event) {


                case "modify":
                    this.dialogInfo.type = "edit";
                    this.is_look = false
                    this.dialogInfo.visible = true;

                    if (data.amount == '0.00') {
                        this.form.amount = ''
                    } else {
                        this.form.amount = data.amount
                    }
                    if (data.remark == null) {
                        this.form.remark = ''
                    } else {
                        this.form.remark = data.remark
                    }
                    this.form.id = data.id
                    // this.form.status = String(data.status)
                    this.form.status = data.status

                    this.$nextTick(() => {
                        this.$refs.form.clearValidate();
                    });

                    break;
                case "look":
                    this.dialogInfo.type = "look";
                    this.is_look = true
                    this.dialogInfo.visible = true;

                    if (data.amount == '0.00') {
                        this.form.amount = ''
                    } else {
                        this.form.amount = data.amount
                    }
                    if (data.remark == null) {
                        this.form.remark = ''
                    } else {
                        this.form.remark = data.remark
                    }
                    this.form.id = data.id
                    // this.form.status = String(data.status)
                    this.form.status = data.status

                    this.$nextTick(() => {
                        this.$refs.form.clearValidate();
                    });

                    break;
                case "save":
                    if (this.dialogInfo.type == "edit") {
                        // 先进行表单验证
                        this.$refs.form.validate((valid) => {
                            if (valid) {
                                // 验证通过后再提交
                                let formData = new FormData();
                                formData.append('id', this.form.id);
                                if (this.form.status == 20) {
                                    formData.append('amount', this.form.amount);
                                }
                                formData.append('status', this.form.status);
                                formData.append('remark', this.form.remark);

                                setFundStatus(formData).then(res => {
                                    if (res.status == 200) {
                                        this.$message({
                                            type: "success",
                                            message: res.msg
                                        });
                                        this.dialogInfo.visible = false;
                                        this.init(this.filterInfo.query);
                                    }
                                });
                            } else {
                                // 验证失败
                                // this.$message({
                                //     type: "error",
                                //     message: "请填写完整的表单信息"
                                // });
                                return false;
                            }
                        });
                    }
                    break;
                case "search":
                    this.pageParm.query.page = 1;
                    this.filterInfo.query.page = 1;
                    this.init(this.filterInfo.query);
                    break;
                case "reset":
                    for (let i in this.filterInfo.query) {
                        this.filterInfo.query[i] = "";
                    }
                    this.filterInfo.query.page = 1;
                    this.filterInfo.query.limit = 20;
                    this.pageParm.query.page = 1;
                    this.pageParm.query.limit = 20;
                    let datas = {};
                    datas = Object.assign({}, this.pageParm.query, this.filterInfo.query);
                    this.init(datas);
                    break;
                case "close":
                    this.dialogInfo.btLoading = false;
                    this.dialogInfo.visible = false;
                    this.form.id = 0;
                    this.form.phone = "";
                    this.form.real_name = "";
                    this.form.pwd = "";
                    this.form.wechat_token = "";
                    this.form.role_id = "";
                    this.form.role_name = "";
                    break;
            }
        },
        // 阻止浏览器默认记忆
        nameMouse (e) {
            if (e.target.localName == 'i') {
                this.$set(this.pagerequest, "empNm", "")
            }
            this.namebool = true;
            setTimeout(() => this.namebool = false, 0)
        },
        onInput (event, val) {
            if (val.length === 0) {
                // this.onBlur(event)
                // this.onFocus(event)
                this.namebool = true;
                setTimeout(() => this.namebool = false, 0)
            }
        },
        handleEvent (event, data, label) {
            let datas = {};
            switch (event) {
                case "rowClick":
                    break;
                case "search":
                    this.pageParm.query.page = 1;
                    this.filterInfo.query.page = 1;
                    this.init(this.filterInfo.query);
                    break;
                // 分页改变当前页
                case "curPage":
                    if (!data) return;
                    this.pageParm.query.page = data;
                    this.filterInfo.query.page = data;
                    datas = Object.assign({}, this.pageParm.query, this.filterInfo.query);
                    this.init(datas);
                    break;
                // 分页列表
                case "pageSize":
                    if (!data) return;
                    this.pageParm.query.limit = data;
                    this.filterInfo.query.limit = data;
                    this.pageParm.query.page = 1;
                    this.filterInfo.query.page = 1;
                    datas = Object.assign({}, this.pageParm.query, this.filterInfo.query);
                    this.init(datas);
                    break;
                default:
                    break;
            }
        },
    },
    // destroyed () {
    //     window.removeEventListener("resize", this.getHeight);
    // }
};
</script>
<style lang="scss">
.user_form {
    padding-left: 40px;
}

.taskList_box_title {
    padding-bottom: 20px;
    line-height: 40px;
}

.taskList_box {
    width: 600px;
    height: 500px;
    overflow-y: auto;
    border: 1px solid #ddd;
    margin-right: -10px;
    border: 1px solid #ddd;
    line-height: 47px;
    width: 623px;

    .el-col {
        height: 60px;
        line-height: 40px;
        padding: 10px 0;

        .el-input-number {
            width: 140px;
        }
    }
}

/* 去掉input尾部上下小箭头 start */
input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
    -webkit-appearance: none !important;
    -moz-appearance: none !important;
    -o-appearance: none !important;
    -ms-appearance: none !important;
    appearance: none !important;
    margin: 0;
}

input[type="number"] {
    -webkit-appearance: textfield;
    -moz-appearance: textfield;
    -o-appearance: textfield;
    -ms-appearance: textfield;
    appearance: textfield;
}

/* 去掉input尾部上下小箭头 end */
</style>
<style lang="scss" scoped>
::v-deep .addFrom .el-cascader--mini {
    width: 360px;
}

::v-deep .el-form-item--medium .el-form-item__label {
    height: 60px;
}

::v-deep .el-form-item--medium .el-form-item__content {
    height: 60px;
}

::v-deep .el-form-item--medium .remark {
    // height: 60px;
    height: auto !important;
}

::v-deep .el-form-item__error {
    color: #ff4949;
    font-size: 12px;
    line-height: 1;
    padding-top: 4px;
    position: absolute;
    top: 60%;
    left: 0;
}

.icon-box {
    display: inline-block;
    font-size: 12px;
    margin-left: 10px;

    span {
        display: inline-block;
        width: 20px;
        height: 20px;
        border-radius: 50%;
        text-align: center;
        line-height: 20px;
        color: #fff;
    }
}

::v-deep .addFrom .el-cascader--mini {
    width: 360px;
}





::v-deep .moneycss {
    width: 120px;
}

.editicon:hover {
    cursor: pointer;
}

.imgggcss {
    display: flex;
    flex-direction: row;
    align-items: center;
}

.excelcss:hover {
    cursor: pointer;
}

::v-deep .excelcss>.el-image__inner {
    vertical-align: middle;
}

.salecss {
    white-space: nowrap;
    /*强制span不换行*/
    display: inline-block;
    /*将span当做块级元素对待*/
    width: 60px;
    /*限制宽度*/
    overflow: hidden;
    /*超出宽度部分隐藏*/
    text-overflow: ellipsis;
    /*超出部分以点号代替*/
    line-height: 0.9;
    /*数字与之前的文字对齐*/
}

.salecss:hover {
    cursor: pointer;
}

.recordTable {
    display: flex;
    flex-direction: column;
}

.concatcss {
    padding-top: 10px;
    padding-left: 10px;
    margin-bottom: 20px;
    position: relative;
    margin-top: 10px;
}

.concatcss_line {
    width: 360px;
    border: 1px dashed #c8b9b9;
}

::v-deep.el-form-item {
    margin-bottom: 0px;
}

::v-deep.concatcss>.el-form-item>.el-form-item__label {
    font-size: 12px !important;
}

::v-deep.concatcss>.el-form-item>.el-form-item__content>.el-input>.el-input__inner {
    font-size: 12px !important;
}

::v-deep.concatcss>.el-form-item>.el-form-item__content>.el-input>.el-input__inner,
.concatcss>.el-form-item>.el-form-item__content {
    height: 30px !important;
    line-height: 30px;
}

::v-deep.concatcss>.el-form-item>.el-form-item__content>.el-select>.el-input>.el-input__inner,
.el-form-item__content>.filter-input>.el-input__inner {
    width: 200px;
    height: 30px;
    font-size: 12px !important;
}

.moredestroyCss {
    ::v-deep.el-form-item {
        margin-bottom: 20px !important;
    }

    ::v-deep .el-input {
        width: 240px !important;
    }

    ::v-deep .el-input__inner {
        width: 240px !important;
    }

    .item {
        width: 330px;
        margin-top: 15px;

        &-title {
            font-size: 13px;
            color: #9BA2AA;
            padding-left: 15px;
            padding-bottom: 5px;
        }

        &-main {
            padding: 10px 15px;
            box-sizing: border-box;
            border-radius: 4px;
            border: 1px dashed #000;

            &-line {
                display: flex;
                align-items: center;
                height: 40px;
                line-height: 40px;
            }

            &-title {
                width: 80px;
                color: #606266;
                font-size: 12px;
                font-weight: 700;

            }

            &-box {
                font-size: 12px;
                color: #606266;

                ::v-deep .el-input__inner {
                    width: 200px !important;
                }

                ::v-deep .el-input {
                    width: 200px !important;
                }
            }
        }
    }
}

.bgfff {
    background: #fff;
    flex: 1;
    display: flex;
    flex-direction: column;
}

.bgfffs {
    background: #fff;
}

.tableHeight {
    flex: 1;
    box-sizing: border-box;
}

.filter_box {
    position: relative;
    // height: 64px;
    overflow: hidden;
    padding-right: 90px;

    .open {
        position: absolute;
        top: 10px;
        right: 20px;
        color: #409EFF;
        cursor: pointer;
    }
}

.app-main {
    padding-top: 90px !important;
}

.container-box {
    background: #f5f7fa;
    display: flex;
    flex-direction: column;
    min-height: 100vh;
    position: relative;
}

::v-deep .pagination-container {
    margin-top: 0 !important;
}

.addicon {
    color: #409eff;
    font-size: 20px;
    vertical-align: middle;
}

.addicon:hover {
    cursor: pointer;
}
</style>